package bank.service.spring.department;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import bank.model.dao.DepartmentDAO;

public class NewDepartmentController implements Controller {
	
	public NewDepartmentController(DepartmentDAO departmentDAO) {
		super();
		this.departmentDAO = departmentDAO;
	}	
	
	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
		System.out.println("This is NewDepartmentController");	
		
		String name = request.getParameter("name");
		String parent = request.getParameter("parent");		
		request.setAttribute("name", name);	

		if (name != null) {						
			departmentDAO.add(name, parent == null ? 1 : Integer.valueOf(parent));
			request.setAttribute("enter", true);
		} else
			throw new Exception("Can't create department without a name");			
		
		return new ModelAndView("department/newdepartment", "name", name);
	}
	
	private DepartmentDAO departmentDAO;
}
